package com.itmk.config.web.sys_menu.controller;

import com.itmk.config.web.sys_menu.entity.MakeMenuTree;
import com.itmk.config.web.sys_menu.entity.SysMenu;
import com.itmk.config.web.sys_menu.service.SysMenuService;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/menu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping
    public ResultVO add(@RequestBody SysMenu sysMenu){
        sysMenu.setCreateTime(new Date());
        if (sysMenuService.save(sysMenu)){
            return ResultUtils.success("新增成功");
        }
        return ResultUtils.error("新增失败");

    }

    @PutMapping
    public ResultVO edit(@RequestBody SysMenu sysMenu){
        sysMenu.setCreateTime(new Date());
        if (sysMenuService.updateById(sysMenu)){
            return ResultUtils.success("编辑成功");
        }
        return ResultUtils.error("编辑失败");

    }

    @DeleteMapping("/{menuId}")
    public ResultVO delete(@PathVariable("menuId") Long menuId){
        if (sysMenuService.removeById(menuId)){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("删除失败");

    }

    //列表
    @GetMapping("/list")
    public ResultVO list(){
        List<SysMenu> list = sysMenuService.list();
        //0L : //把长整形数i赋值0
        List<SysMenu> menuList = MakeMenuTree.makeTree(list,0L);
        return ResultUtils.success("查询成功",menuList);

    }

    //列表
    @GetMapping("/parent")
    public ResultVO getParent(){
        List<SysMenu> list = sysMenuService.getParent();
        return ResultUtils.success("查询成功",list);

    }
}
